
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import javax.swing.AbstractAction;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;

import org.rsbot.loader.asm.ClassAdapter;
import org.rsbot.loader.asm.ClassReader;
import org.rsbot.script.Script;
import org.rsbot.script.ScriptManifest;


@ScriptManifest(authors = { "Harry Andreas" }, keywords = "Script Loader", name = "Source9ScriptLoader", version = 1.0, description = "The script loader from source9!")
public class Source9ScriptLoader extends Script {

	public LoginGUI gui;
	public ScriptSelector script1;
	
	public Socket s;
	
	public DataInputStream in;
	public DataOutputStream out;
	
	public boolean scriptStarted = false;
	
	public String url;
	
	public double version = 1.0;
	
	public boolean onStart() {
		URL website;
		double vers = 0;
		try {
			website = new URL("http://www.source9.org/version.txt");
	        URLConnection connection = website.openConnection();
	        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
	        vers = Double.parseDouble(in.readLine());
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if(vers > version) {
			log.info("Outdated version of Source9ScriptLoader. Version: "+vers+" is availble for download!");
			JOptionPane.showMessageDialog(
					null, "The loader you are using is currently outdated. \n" +
					" Please run the script loader's updater. If you don't have it \n" +
					"you can download it from http://www.source9.org", "Source9ScriptLoader Updater", 
					JOptionPane.INFORMATION_MESSAGE);
		} else {
			log.info("You are using the latest version of the Source9ScriptLoader!");
		}
        mouse.setSpeed(5);
		script1 = new ScriptSelector();
		gui = new LoginGUI();
		gui.setVisible(true);
		return true;
	}
	
	public String username, password;
	public int count;
	
	public void onFinish() {
		try {
			gui.dispose();
			script1.dispose();
			if(c != null)
				c.onFinish();
		} catch(Exception e) {
			
		}
	}
	
	
	public void requestScript() throws IOException {
		sendScriptRequest();
		while(!gui.urlRecieved) {
		}
		loadScript(url);
	}
	
	public void sendScriptRequest() {
		try {
			out.writeByte(2);
			out.writeUTF(script1.scriptSelected);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public class LoginGUI extends JFrame {
		
		/**
		 * 
		 */
		private static final long serialVersionUID = -3351205633735804175L;

		public LoginGUI() {
			initComponents();
		}
		
		public boolean loginClicked = false;
		
		public void connect(String user, String pass) throws UnknownHostException, IOException, InterruptedException {
			if((s == null) || s.isClosed()) {
				s = new Socket(InetAddress.getByName( "127.0.0.1" ), 50 );
			}
			label5.setText("Connected to server...");			
			in = new DataInputStream(s.getInputStream());
			out = new DataOutputStream(s.getOutputStream());
			out.writeByte(1);
			out.writeUTF(user);
			out.writeUTF(pass);
			label5.setText("Authenticating...");
			startProcessForNetworking();
		}
		
		
		
		public void processLogin() {
			if(loginClicked)
				return;
			loginClicked = true;
			textField1.setEnabled(false);
			passwordField1.setEnabled(false);
			button1.setEnabled(false);
			label5.setText("Connecting to server...");
			char[] crypted = passwordField1.getPassword();
			String pass = "";
			for(char s : crypted) {
				pass = pass + s;
			}
			username = textField1.getText();
			password = pass;
			final String pass1 = pass;
				executors.submit(new Runnable() {

					@Override
					public void run() {
						try {
							connect(textField1.getText(), pass1);
						} catch (UnknownHostException e) {
							
						} catch (IOException e) {
							
						} catch (InterruptedException e) {
							
						}
					}
				});
			
		}
		
		public void startProcessForNetworking() throws IOException, InterruptedException {
			while(s.isConnected()) {
				out.writeByte(0);
				byte opcode = in.readByte();
				int i = 0;
				int ii = 0;
				if(opcode == 1) {
					i = in.readInt();
					ii = in.readInt();
					log.info("Recieved: "+i);
					if(i == 2) {
						String accountType = "";
						switch(ii) {
						case 1:
							accountType = "PREMIUM";
							break;
						case 2: 
							accountType = "EXTREME";
							break;
						case 3:
							accountType = "FREE";
							script1.disablePremiumScripts();
							break;
						}
						label5.setText("Login succesful.");
						script1.label2.setText("Logged in as: "+username);
						script1.label3.setText("Account Type: "+accountType);
						gui.setVisible(false);
						script1.setVisible(true);
					}
					if(i == 1) {
						String s = "";
						switch(ii) {
							case -1:
								s = "Account Disabled, see forums!";
								break;
							case 4:
								s = "You account does not exist!";
							break;
							case 5:
								s = "Password is invalid!";
								break;
						}
						label5.setText("Login attempt failed. "+s);
						textField1.setEnabled(true);
						passwordField1.setEnabled(true);
						button1.setEnabled(true);
						loginClicked = false;
						return;
					}
				}
				if(opcode == 2) {
					url = in.readUTF();
					urlRecieved = true;
				}
				Thread.sleep(500);
			}
			log.info("Socket connected? "+s.isConnected());
		}
		
		public boolean urlRecieved = false;
		

		private void initComponents() {
			// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
			// Generated using JFormDesigner Evaluation license - Harry Andreas
			label1 = new JLabel();
			label2 = new JLabel();
			label3 = new JLabel();
			textField1 = new JTextField();
			passwordField1 = new JPasswordField();
			button1 = new JButton();
			button2 = new JButton();
			label4 = new JLabel();
			label5 = new JLabel();
			action1 = new CancelButtonPressed();
			action2 = new LoginButtonClicked();

			//======== this ========
			Container contentPane = getContentPane();

			//---- label1 ----
			label1.setText("Please login to the Source9 Script Loader");
			label1.setFont(label1.getFont().deriveFont(label1.getFont().getStyle() | Font.BOLD));

			//---- label2 ----
			label2.setText("Username:");
			label2.setFont(label2.getFont().deriveFont(label2.getFont().getStyle() | Font.BOLD));

			//---- label3 ----
			label3.setText("Password:");
			label3.setFont(label3.getFont().deriveFont(label3.getFont().getStyle() | Font.BOLD));

			//---- textField1 ----
			textField1.setToolTipText("Enter your username here");

			//---- passwordField1 ----
			passwordField1.setToolTipText("Enter your password here");

			//---- button1 ----
			button1.setAction(action2);

			//---- button2 ----
			button2.setAction(action1);

			//---- label4 ----
			label4.setText("Note: Login with your forums name and password");
			label4.setFont(label4.getFont().deriveFont(label4.getFont().getStyle() | Font.BOLD));

			GroupLayout contentPaneLayout = new GroupLayout(contentPane);
			contentPane.setLayout(contentPaneLayout);
			contentPaneLayout.setHorizontalGroup(
				contentPaneLayout.createParallelGroup()
					.addGroup(contentPaneLayout.createSequentialGroup()
						.addGroup(contentPaneLayout.createParallelGroup()
							.addGroup(contentPaneLayout.createSequentialGroup()
								.addContainerGap()
								.addComponent(label4, GroupLayout.DEFAULT_SIZE, 290, Short.MAX_VALUE))
							.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
								.addGroup(GroupLayout.Alignment.LEADING, contentPaneLayout.createSequentialGroup()
									.addGap(30, 30, 30)
									.addComponent(label5, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
								.addGroup(GroupLayout.Alignment.LEADING, contentPaneLayout.createSequentialGroup()
									.addGap(22, 22, 22)
									.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
										.addComponent(label1, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE)
										.addGroup(GroupLayout.Alignment.LEADING, contentPaneLayout.createSequentialGroup()
											.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
												.addComponent(label3, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
												.addComponent(label2, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
											.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
											.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
												.addComponent(passwordField1)
												.addComponent(textField1, GroupLayout.DEFAULT_SIZE, 147, Short.MAX_VALUE))
											.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE))
										.addComponent(button2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
										.addComponent(button1, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
						.addContainerGap())
			);
			contentPaneLayout.setVerticalGroup(
				contentPaneLayout.createParallelGroup()
					.addGroup(contentPaneLayout.createSequentialGroup()
						.addContainerGap()
						.addComponent(label1)
						.addGap(18, 18, 18)
						.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
							.addComponent(label2)
							.addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
						.addGroup(contentPaneLayout.createParallelGroup()
							.addComponent(label3)
							.addComponent(passwordField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGap(11, 11, 11)
						.addComponent(label5, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(button1)
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(button2)
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(label4, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(16, Short.MAX_VALUE))
			);
			pack();
			setLocationRelativeTo(getOwner());
			// JFormDesigner - End of component initialization  //GEN-END:initComponents
		}

		// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
		// Generated using JFormDesigner Evaluation license - Harry Andreas
		private JLabel label1;
		private JLabel label2;
		private JLabel label3;
		private JTextField textField1;
		private JPasswordField passwordField1;
		private JButton button1;
		private JButton button2;
		private JLabel label4;
		private JLabel label5;
		private CancelButtonPressed action1;
		private LoginButtonClicked action2;
		// JFormDesigner - End of variables declaration  //GEN-END:variables

		private class CancelButtonPressed extends AbstractAction {
			private CancelButtonPressed() {
				// JFormDesigner - Action initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
				// Generated using JFormDesigner Evaluation license - Harry Andreas
				putValue(NAME, "Cancel");
				putValue(SHORT_DESCRIPTION, "Cancel");
				// JFormDesigner - End of action initialization  //GEN-END:initComponents
			}

			public void actionPerformed(ActionEvent e) {
				if(loginClicked) {
					textField1.setEnabled(true);
					passwordField1.setEnabled(true);
					button1.setEnabled(true);
					label5.setText("");
					loginClicked = false;
					return;
				}
				gui.setVisible(false);
				gui = null;
				executors = null;
				stopScript();
			}
		}

		private class LoginButtonClicked extends AbstractAction {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1200496868865626164L;

			private LoginButtonClicked() {
				// JFormDesigner - Action initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
				// Generated using JFormDesigner Evaluation license - Harry Andreas
				putValue(NAME, "Login");
				// JFormDesigner - End of action initialization  //GEN-END:initComponents
			}

			public void actionPerformed(ActionEvent e) {
				processLogin();
			}
		}
	}
	
	public class ScriptSelector extends JFrame {
		
		public ScriptSelector() {
			initComponents();
		}

		private void initComponents() {
			// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
			// Generated using JFormDesigner Evaluation license - Harry Andreas
			label1 = new JLabel();
			button1 = new JButton();
			button2 = new JButton();
			button3 = new JButton();
			button4 = new JButton();
			button5 = new JButton();
			button6 = new JButton();
			label2 = new JLabel();
			label3 = new JLabel();
			label4 = new JLabel();
			button7 = new JButton();
			button8 = new JButton();
			action1 = new AutoCookerClicked();
			action2 = new AutoAlcherPro();
			action3 = new AutoHerbCleanerProClicked();
			action4 = new LoadScript();

			//======== this ========
			Container contentPane = getContentPane();

			//---- label1 ----
			label1.setText("Please select a script from the menu below!");
			label1.setFont(label1.getFont().deriveFont(label1.getFont().getStyle() | Font.BOLD, label1.getFont().getSize() + 5f));

			//---- button1 ----
			button1.setAction(action1);

			//---- button2 ----
			button2.setAction(action2);

			//---- button3 ----
			button3.setAction(action3);
			button3.setFont(button3.getFont().deriveFont(button3.getFont().getStyle() | Font.PLAIN, label1.getFont().getSize() - 5f));
			button3.setText("AutoHerbCleanerPro");

			//---- button4 ----
			button4.setText("BlankScript");
			button4.setEnabled(false);
			button4.setToolTipText("This script has not yet been developed");

			//---- button5 ----
			button5.setText("BlankScript");
			button5.setEnabled(false);
			button5.setToolTipText("This script has not yet been developed");

			//---- button6 ----
			button6.setText("BlankScript");
			button6.setEnabled(false);
			button6.setToolTipText("This script has not yet been developed");

			//---- label2 ----
			label2.setText("Logged in as:");

			//---- label3 ----
			label3.setText("Account Type:");

			//---- label4 ----
			label4.setText("Script Selected:");

			//---- button7 ----
			button7.setAction(action4);

			//---- button8 ----
			button8.setText("Close");

			GroupLayout contentPaneLayout = new GroupLayout(contentPane);
			contentPane.setLayout(contentPaneLayout);
			contentPaneLayout.setHorizontalGroup(
				contentPaneLayout.createParallelGroup()
					.addGroup(contentPaneLayout.createSequentialGroup()
						.addContainerGap()
						.addGroup(contentPaneLayout.createParallelGroup()
							.addComponent(label1, GroupLayout.DEFAULT_SIZE, 490, Short.MAX_VALUE)
							.addGroup(contentPaneLayout.createSequentialGroup()
								.addComponent(button1, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(button2, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(button3, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE))
							.addGroup(contentPaneLayout.createSequentialGroup()
								.addComponent(button4, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(button5, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(button6, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE))
							.addGroup(contentPaneLayout.createSequentialGroup()
								.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
									.addComponent(label3, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
									.addComponent(label2, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE))
								.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
								.addGroup(contentPaneLayout.createParallelGroup()
									.addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
										.addComponent(label4, GroupLayout.PREFERRED_SIZE, 236, GroupLayout.PREFERRED_SIZE)
										.addGap(77, 77, 77))
									.addGroup(contentPaneLayout.createSequentialGroup()
										.addComponent(button7, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
										.addComponent(button8, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE)))))
						.addContainerGap())
			);
			contentPaneLayout.setVerticalGroup(
				contentPaneLayout.createParallelGroup()
					.addGroup(contentPaneLayout.createSequentialGroup()
						.addComponent(label1, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
						.addGap(18, 18, 18)
						.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
							.addComponent(button1, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
							.addComponent(button2, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
							.addComponent(button3, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
						.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
							.addComponent(button4, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
							.addComponent(button5, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
							.addComponent(button6, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE))
						.addGap(18, 18, 18)
						.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
							.addComponent(label2, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
							.addComponent(label4, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
						.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
							.addComponent(label3, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE)
							.addComponent(button7)
							.addComponent(button8))
						.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
			);
			pack();
			setLocationRelativeTo(getOwner());
			// JFormDesigner - End of component initialization  //GEN-END:initComponents
		}

		// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
		// Generated using JFormDesigner Evaluation license - Harry Andreas
		private JLabel label1;
		private JButton button1;
		private JButton button2;
		private JButton button3;
		private JButton button4;
		private JButton button5;
		private JButton button6;
		private JLabel label2;
		private JLabel label3;
		private JLabel label4;
		private JButton button7;
		private JButton button8;
		private AutoCookerClicked action1;
		private AutoAlcherPro action2;
		private AutoHerbCleanerProClicked action3;
		private LoadScript action4;
		// JFormDesigner - End of variables declaration  //GEN-END:variables
		
		
		public String scriptSelected = "";

		private class AutoCookerClicked extends AbstractAction {
			private AutoCookerClicked() {
				// JFormDesigner - Action initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
				// Generated using JFormDesigner Evaluation license - Harry Andreas
				putValue(NAME, "AutoCookerPro");
				putValue(SHORT_DESCRIPTION, "The superb, flawless AutoCookerPro brought to you freely by Source9.org");
				// JFormDesigner - End of action initialization  //GEN-END:initComponents
			}

			public void actionPerformed(ActionEvent e) {
				scriptSelected = "AutoCookerPro";
				label4.setText("Script Selected: "+scriptSelected);
			}
		}

		private class AutoAlcherPro extends AbstractAction {
			/**
			 * 
			 */
			private static final long serialVersionUID = -2375168610451040184L;

			private AutoAlcherPro() {
				// JFormDesigner - Action initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
				// Generated using JFormDesigner Evaluation license - Harry Andreas
				putValue(NAME, "AutoAlcherPro");
				// JFormDesigner - End of action initialization  //GEN-END:initComponents
			}

			public void actionPerformed(ActionEvent e) {
				scriptSelected = "AutoAlcherPro";
				label4.setText("Script Selected: "+scriptSelected);
			}
		}

		private class AutoHerbCleanerProClicked extends AbstractAction {
			/**
			 * 
			 */
			private static final long serialVersionUID = 5174642889737988521L;

			private AutoHerbCleanerProClicked() {
				// JFormDesigner - Action initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
				// Generated using JFormDesigner Evaluation license - Harry Andreas
				putValue(NAME, "AutoHerbCleanerPro");
				// JFormDesigner - End of action initialization  //GEN-END:initComponents
			}

			public void actionPerformed(ActionEvent e) {
				scriptSelected = "AutoHerbCleanerPro";
				label4.setText("Script Selected: "+scriptSelected);
			}
		}

		private class LoadScript extends AbstractAction {
			/**
			 * 
			 */
			private static final long serialVersionUID = -3415858468257927026L;

			private LoadScript() {
				// JFormDesigner - Action initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
				// Generated using JFormDesigner Evaluation license - Harry Andreas
				putValue(NAME, "Load Script");
				// JFormDesigner - End of action initialization  //GEN-END:initComponents
			}

			public void actionPerformed(ActionEvent e) {
				button1.setEnabled(false);
				button2.setEnabled(false);
				button3.setEnabled(false);
				button7.setEnabled(false);
				try {
					requestScript();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		}
		
		public void disablePremiumScripts() {
			this.button3.setEnabled(false);
			this.button3.setToolTipText("Please upgrade to a premium user to enable access to this script!");
		}
	}
	
	
	/**
	 * The executor pool
	 */
	public ExecutorService executors = Executors.newCachedThreadPool();
	
	@Override
	public int loop() {
		if(c != null) {
			if(scriptStarted)
				return c.loop();
		}
		return 500;
	}
	
	public Script c = null;
	
	public void loadScript(String url) throws IOException {
		URL classUrl;
	 	classUrl = new URL("jar:"+url);
        URL[] classUrls = { classUrl };
        URLClassLoader ucl = new URLClassLoader(classUrls);
        try {
			c = (Script) ucl.loadClass(script1.scriptSelected).newInstance();
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		script1.dispose();
		gui.dispose();
		s.close();
		in.close();
		out.close();
		executors.submit(new Runnable() {
			@Override
			public void run() {
				delegateTo(c);
				c.onStart();
			}
		});
		
		scriptStarted = true;
	}
}
